package com.googlecode.phpunit4eclipse.j2phpunit.runner;

import com.googlecode.phpunit4eclipse.j2phpunit.runner.api.Failure;
import com.googlecode.phpunit4eclipse.j2phpunit.runner.api.PHPUnitTestDescription;
import com.googlecode.phpunit4eclipse.j2phpunit.runner.api.RunNotifier;

public class Run {

	/**
	 * @param args
	 */
	public static void main(String directory) throws Exception {
		PHPUnitRunner runner = new PHPUnitRunner(directory, ".");

		for(Class<?> clazz : runner.getChildren()) {
			runner.runChild(clazz, new Notifier());
		}
	}
	
	public static String getName(PHPUnitTestDescription phpUnitTestDescription) {
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("[ ");
		PHPUnitTestDescription current = phpUnitTestDescription;
		while(current != null) {
			stringBuffer.append(" / ");
			stringBuffer.append(current.getName());
			current = current.getParent();
		}
		stringBuffer.append(" ] ");
		return stringBuffer.toString();
	}
	
	static class Notifier implements RunNotifier {

		public void fireTestFailure(Failure failure) {
			System.out.println(getName(failure.getDescription()) + "failure");
			failure.getException().printStackTrace(System.err);
			
		}

		public void fireTestFinished(PHPUnitTestDescription description) {
			System.out.println(getName(description) + "OK");
		}

		public void fireTestStarted(PHPUnitTestDescription description) {
			System.out.println(getName(description) + "Start");	
		}
		
	}
	

}
